﻿using System.Web.Mvc;
using MedicalLog.Domain.Repositories;
using MedicalLog.Domain.Entities;
using System.Web.Security;
using MedicalLog.WebUI.Models;
using System.Net.Mail;

namespace MedicalLog.WebUI.Controllers
{
    public class DoctorController : Controller
    {
        
        DoctorRepository docRepository;

        public DoctorController()
        {
            docRepository = new DoctorRepository();
        }


        public ActionResult Details(int id)
        {
            return View(docRepository.DetailDoctor(id));
        }

        //
        // GET: /Doctor/Create

        public ActionResult AddDoctor()
        {
            return View();
        }

        //
        // POST: /Doctor/Create

        [HttpPost]
        public ActionResult AddDoctor(Doctor doctor)
        {
            if (ModelState.IsValid)
            {
                docRepository.AddDoctor(doctor);
                return RedirectToAction("Detail", new {id = doctor.IdDoctor });

            }
            else
            {
                return View(doctor);
            }
        }

        //
        // GET: /Doctor/Edit/5

        public ActionResult Edit(int id)
        {
            return View(docRepository.UpdateDoctor(id));
        }

        //
        // POST: /Doctor/Edit/5

        [HttpPost]
        public ActionResult Edit(Doctor doctor)
        {
            if (ModelState.IsValid)
            {
                docRepository.UpdateDoctor(doctor);
                return RedirectToAction("Detail", new { id = doctor.IdDoctor });
            }
            else
            {
                return View(doctor);
            }

        }

        //
        // GET: /Doctor/Delete/5

        public ActionResult Delete(int id)
        {
            return View(docRepository.DeleteDoctor(id));
        }

        //
        // POST: /Doctor/Delete/5

        [HttpPost]
        public ActionResult Delete(Doctor doctor)
        {
            docRepository.DeleteDoctor(doctor);
            return RedirectToAction("SearchPatient", "Patient");
        }

        public ActionResult Detail(int id)
        {
            return View(docRepository.DetailDoctor(id));
        }

        [HttpGet]
        public ActionResult Login()
        {
            return View();
        
        }

        [HttpPost]
        public ActionResult Login(LoginViewModel pModel)
        {
            if (ModelState.IsValid)
            {
                if (docRepository.Login(pModel.Username,pModel.Password))
                {

                    FormsAuthentication.SetAuthCookie(pModel.Username, false);
                    return RedirectToAction("SearchPatient", "Patient");
                    

                }
                else
                {

                    ModelState.AddModelError("Wrong credentials", "Nombre de usuario o contraseña introducida no es correcta.");
                    
                }
            }


            return View();
            

        }

        [HttpGet] 
        public ActionResult RecoverPassword()
        {
            return View();
        
        }

        public ActionResult RecoverPassword(LoginViewModel pModel)
        {
            docRepository.RecoverPassword(pModel.Email);
            return RedirectToAction("Login");
                       
        
        }

    }
}
